<?php  
	
	namespace Home\Controller;
	use Think\Controller;
	
	class ComplainStuController extends AllowController{
        // stu事务处理的列表查询
        public function indexstu(){
            //实例化
            $mod = M('complain');
            $mods = M('users');
            //获取自己登入的id
            $id=$_SESSION['userid'];
            $tot = $mod->where("type=0 AND sender=$id")->Count();
            
            $nums = 5;
            $page = new \Think\Page($tot,$nums);
            
            $list = $mod->limit($page->firstRow,$page->listRows)->order('sendtime desc')->where("type=0 AND sender=$id")->select();

            //遍历 把时间戳转换日期
            foreach($list as $key=>$value){
                $list[$key]['sendtime'] = date("Y-m-d H:i:s",$value['sendtime']);

                //通过sender的用户id去关联users查询用户姓名
                $name = $mods->where("id={$list[$key]['sender']}")->find();
                $list[$key]['sender'] = $name["name"];

                // var_dump($list[$key]['conductor']);
                //通过conductor 去关联users查询用户姓名
                $conname = $mods->where("id={$list[$key]['conductor']}")->find();
                // echo $mods->getLastSql($conname);
                $list[$key]['conductor'] = $conname["name"];
            }
            if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
            //分配数据
            $this->assign('list',$list);
            $this->assign('curr',$p);
            $this->assign('page',ceil($tot/$nums));
            $this->assign('total',$tot);

            //导入模版
            $this->display("Complain/stucomplain");
        }

        //显示look模块
        public function lookstu(){
            $a = I('get.id');

            //实例化
            $mod = M('complain');
            $mods = M("users");           
            
            //查询单条语句
            $list=$mod->find($a);

            //获取发送人id
            $b = $list["sender"];
            
            //查询users下$b的name值
            $lists=$mods->field("name")->find($b);

            //赋值
            $list["sender"] = $lists['name'];
            //分配数据
            $this->assign('list',$list);
            $this->display('Complain/stulook');
        }
        
        //查看事务的信息
        public function add(){
            $names=$_SESSION['name'];

            $this->assign("names",$names);
            $this->display('Complain/add');
        }

        //插入数据
        public function insert(){
            //创建一个空数组
            // $data = array();
            // 接受Ajax来的数据 
            $data['sender']=$_SESSION['userid'];
            $data['type'] = 0;
            $data['contents'] = $_POST['contents'];
            $data['status'] = 0;
            //获取时间戳
            $data['sendtime']=time();

            //实例化
            $mod = M("complain");

            if($mod->add($data)){
                echo "添加成功";
            }else{
                echo "添加失败";
            }             
        }		    
    }
?>
